package com.zc.imooc.finaltest.kafka.config;

import lombok.Data;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.Properties;

@Component
@Data
public class MyProducerConfig {

    @Autowired
    private MyProducerProperties myProducerProperties;

    @Bean
    public Producer getProducer() {
        Properties properties = new Properties();
        properties.setProperty(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, myProducerProperties.getBootstrapServersConfig());
        properties.setProperty(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, myProducerProperties.getKeySerializerClassConfig());
        properties.setProperty(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, myProducerProperties.getValueSerializerClassConfig());
        properties.setProperty(ProducerConfig.ACKS_CONFIG, myProducerProperties.getAcksConfig());
        properties.setProperty(ProducerConfig.RETRIES_CONFIG, myProducerProperties.getRetriesConfig());
        properties.setProperty(ProducerConfig.BATCH_SIZE_CONFIG, myProducerProperties.getBatchSizeConfig());
        properties.setProperty(ProducerConfig.LINGER_MS_CONFIG, myProducerProperties.getLingerMsConfig());
        properties.setProperty(ProducerConfig.BUFFER_MEMORY_CONFIG, myProducerProperties.getBufferMemoryConfig());
//        properties.setProperty(ProducerConfig.PARTITIONER_CLASS_CONFIG,myProducerProperties);// 自定义分区
        return new KafkaProducer<String, String>(properties);
    }

}
